﻿

$$.PageBase = function () {
	var me = this;
	me.MustLogon = true;	//必须登录
	me.IsPresist = false;	//关闭后也一直保留不被删除
	me.IsDialog = false;	//是弹出窗口，发起窗口不关闭

	function emptyFunc() { }
	var frm = window.frameElement || {};
	var dispose = frm.Close || function () { $(frm).remove();};
	me.Params = frm.Params || {};
	me.Callback = frm.Callback || emptyFunc;
	me.Ready = frm.Ready || emptyFunc;

	me.Node = document.body || document.documentElement;

	var curTips = null;
	me.Tips = function (mess, second, animatenone, position) {
		if (!mess) return;
		if (curTips) { curTips.Close(); }
		var box = $("<div>", { "class": "Tips" });
		mess = (mess + "").replace(/\n/g, "<br />"); box.html(mess);
		box.Close = function () { if (box) { box.remove(); box = null; } };
		if (!(second < 0)) box.onClick(box.Close);
		box.appendTo(document.body || document.documentElement);
		if (!second || second < 1) second = 3;
		window.setTimeout(box.Close, second * 1000);
		if (position) {
			box.css('top', position + 'px');
			if (animatenone === true) box.animate({ top: (position - 30) + "px" }, 100);
		} else {
			if (animatenone === true) box.animate({ top: "30px" }, 100);
		}

		return curTips = box;
	};
	me.Tips.Close = function () {
		if (!curTips) return;
		curTips.Close();
		curTips = null;
	};

	me.Dialog = function (mess, title, buttons) {
		var dialog = $("<div>", { "class": "Dialog" });
		function close() { if (dialog) { dialog.remove(); dialog = null; } };
		dialog.Close = close;
		var arr = [];

		function btn(name, func) {
			var ev = func ? name || "" : "";
			name = name || "关闭";
			arr.push("<td data-evt='" + ev + "'><button>");
			arr.push(name);
			arr.push("</button></td>");
		}
		function btns() {
			if (!buttons || buttons.length < 1) { btn(); return; }
			if ($.type(buttons) === "string") { btn(buttons); return; }
			for (var n in buttons) {
				if (!n) btn();
				else btn(n, buttons[n]);
			}
		}

		arr.push("<div class='box'>");
		if (title) {
			arr.push("<h3 class='title'>");
			arr.push(title);
			arr.push("</h3>");
		}

		arr.push("<div class='cont'>");
		arr.push($$.FixHtml(mess + "").replace(/\n/g, "<br />"));
		arr.push("</div>");

		arr.push("<table class='btns'><tr>");
		btns();
		arr.push("</tr></table>");
		arr.push("</div>");
		dialog.html(arr.join(""));
		$("[data-evt]", dialog).each(function () {
			var ev = this.dataset.evt;
			if (!ev) { $(this).onClick(close); return; }
			var func = buttons[ev];
			if (func) $(this).onClick(function () { close(); func(); });
			else $(this).onClick(close);
		});
		dialog.appendTo(document.body || document.documentElement);
		return dialog;
	};

	var waitNode = null, waitTimer = null;
	me.Wait = function (second) {
		if(!waitNode) waitNode = $("<div>", { "class": "Waiting" });
		waitNode.css("display", "block");
		if (second > 0) waitTimer = setTimeout(me.Wait.Close, second * 1000);
	};
	me.Wait.Close = function () {
		if (waitTimer) { clearTimeout(waitTimer); waitTimer = null; }
		if(waitNode) waitNode.css("display", "none");
	};


	//initbackcall(true/other)
	//return:true/false/other
	me.Init = function (initbackcall) { return true; };
	me.RenderOk = function () { me.Wait.Close();};
	me.Render = function () { me.RenderOk(); };
	me.OnClose = emptyFunc;
	me.Close = function () {
		if(me.OnClose() === false) return;
		dispose();
	};


	function run0() {
		me.Tpl = new $$.Tpl(me);
		try{
			me.Render(); 
		} catch(err){
			console.warn("Render出错：%s",document.baseURI);
			console.error(err);
			dispose();
		}
		me.Ready(me);
	}
	function run() {
		var rt = me.Init(function (rst) {
			if (rt === true || rt === false) return;
			if (rst === true) { run0(); return; }
			me.Close();
		});
		if (rt === true) run0();
		else if (rt === false) me.Close();
	}
	me.start = function () {
	    if (me.MustLogon && !$$.Vis.IsLogon()) {//
			$$.Page.Open("login", null, me.start);
		}
	    else run();
	};
};
